package org.springblade.modules.client;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springblade.common.annotation.ServiceLog;
import org.springblade.common.constant.TrainingOperateConstant;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.feedback.entity.FeedbackEntity;
import org.springblade.modules.feedback.service.IFeedbackService;
import org.springblade.modules.feedback.vo.FeedbackVO;
import org.springblade.modules.feedback.wrapper.FeedbackWrapper;
import org.springblade.modules.library.entity.MaterialLibraryEntity;
import org.springblade.modules.library.service.IMaterialLibraryService;
import org.springblade.modules.personal.entity.SecrecyLevel;
import org.springblade.modules.resource.builder.oss.OssBuilder;
import org.springblade.modules.resource.enums.AttachType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * 客户端 用户反馈控制器
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-client/feedback")
@Api(value = "客户端用户反馈相关接口", tags = "用户反馈相关接口")
public class ClientFeedbackController {

	private final IFeedbackService feedbackService;
	private final OssBuilder ossBuilder;
	private final IMaterialLibraryService materialLibraryService;
	/**
	 * 反馈表 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "需传参")
	public R<FeedbackVO> detail(FeedbackEntity feedback) {
		FeedbackEntity detail = feedbackService.getOne(Condition.getQueryWrapper(feedback));
		return R.data(FeedbackWrapper.build().entityVO(detail));
	}
	/**
	 * 反馈表 分页
	 * status 处理状态0 待处理，1已处理
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "需传参")
	public R<IPage<FeedbackVO>> list(@ApiIgnore @RequestParam Map<String, Object> feedback, Query query) {
		feedback.put("createUser", AuthUtil.getUserId());
		IPage<FeedbackEntity> pages = feedbackService.page(Condition.getPage(query), Condition.getQueryWrapper(feedback, FeedbackEntity.class));
		return R.data(FeedbackWrapper.build().pageVO(pages));
	}

	/**
	 * 反馈表 新增
	 */
	@ServiceLog(value = TrainingOperateConstant.FEEDBACK,operate = TrainingOperateConstant.ADD,describe = "用户反馈")
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "需传参")
	public R save(@Valid @RequestBody FeedbackEntity feedback) {
		feedback.setStatus(0);//待处理
		return R.status(feedbackService.save(feedback));
	}

	/**
	 * 反馈的图片、视频文件上传
	 * @return ObjectStat
	 */
	@ServiceLog(value = TrainingOperateConstant.FEEDBACK, operate = TrainingOperateConstant.ADD, describe = "用户反馈的资源上传")
	@SneakyThrows
	@ApiOperationSupport(order = 10)
	@PostMapping("/upload-file")
	@ApiOperation(value = "反馈的资源上传", notes = "反馈的资源上传")
	public R<String> questionPutFile(MultipartFile file) {
		String fileName =file.getOriginalFilename();
		BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
		MaterialLibraryEntity entity = materialLibraryService.buildAttach(fileName, file.getSize(), bladeFile,
			AttachType.QUESTION_IMG.getCategory(), SecrecyLevel.NORMAL.getCode(),0);
		return R.data(entity.getStoragePath());
	}

}
